/*jslint white: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxerr: 14 */
/*global window: false, REDIPS: true, $: true */

/* enable strict mode */
"use strict";

// define functions
var redips_init,
	load_table,
	rd;

var dataArray = new Array();
// redips initialization
redips_init = function () {
    // reference to the REDIPS.drag lib
    var msg = document.getElementById('message');
    rd = REDIPS.drag;
    // initialization
    rd.init();
    rd.trash_ask = false;

    REDIPS.drag.drop_option = 'single';
    //    rd.mark.exception.placec0 = 'place_cell';
    //    rd.mark.exception.people0 = 'people_cell';
    //    rd.mark.exception.event0 = 'event_cell';
    //    rd.mark.exception.activity0 = 'activity_cell';
    //rd.only.div.activity = 'last';
    //	for (var i = 2; i < 9; i++) {
    //	    var tst = 'greenc' + i;
    //	    rd.mark.exception.tst;
    //	}




    rd.myhandler_clicked = function () {
        msg.innerHTML = 'Clicked';
        // $('#fancybox-title-inside').append('Click'); // ***
    };
    rd.myhandler_dblclicked = function () {
        msg.innerHTML = 'Dblclicked';
        dataArray.push($("#" + rd.obj.id).attr("obj"));
    };
    rd.myhandler_moved = function () {
        msg.innerHTML = 'Moved';
    };
    rd.myhandler_notmoved = function () {
        msg.innerHTML = 'Not moved';
    };
    rd.myhandler_dropped = function () {
        if (-1 == jQuery.inArray($("#" + rd.obj.id).attr("obj"), dataArray)) {
            dataArray.push($("#" + rd.obj.id).attr("obj"));
            $("#" + rd.obj.id).attr("dropped","1");
            $("#content ul").append('<li id="li_' + rd.obj.id + '" onclick="DeleteNodes(this,' + rd.obj.id + ');" uid="' + $("#" + rd.obj.id).attr("uid") + '" type="' + $("#" + rd.obj.id).attr("type") + '"><a href="#!/"><img src="../Objects/' + $("#" + rd.obj.id).attr("type") + '/' + $("#" + rd.obj.id).attr("uid") + '" wdith="40" height="40" /></a></li>');
        }
        else if ($("#" + rd.obj.id).attr("dropped") != "1") {
            $("#" + rd.obj.id).remove();
        }
        //$('#contentList').append(rd.obj.id + ' ');
        //msg.innerHTML = 'Dropped a ' + $("#" + rd.obj.id).attr("obj") + ' >> ' + rd.obj.id;
    };
    rd.myhandler_switched = function () {
        msg.innerHTML = 'Switched';
    };
    rd.myhandler_clonedend1 = function () {
        msg.innerHTML = 'Cloned end1';
    };
    rd.myhandler_clonedend2 = function () {
        msg.innerHTML = 'Cloned end2';
    };
    rd.myhandler_notcloned = function () {
        msg.innerHTML = 'Not cloned';
    };
    rd.myhandler_deleted = function (cloned) {
        // if cloned element is directly moved to the trash
        if (cloned) {
            // set id of original element (read from redips property)
            // var id_original = rd.obj.redips.id_original;
            msg.innerHTML = 'Deleted (c)';
        }
        else {
            msg.innerHTML = 'Deleted' + ' ' + rd.obj.id;
            dataArray.splice(dataArray.indexOf($("#" + rd.obj.id).attr("obj")), 1);
            $("#li_" + rd.obj.id).remove();
        }
    };
    rd.myhandler_undeleted = function () {
        msg.innerHTML = 'Undeleted';
    };
    rd.myhandler_cloned = function () {
        // display message
        msg.innerHTML = 'Cloned';
        // append 'd' to the element text (Clone -> Cloned)
        // rd.obj.innerHTML += 'd';
    };
    rd.myhandler_changed = function () {
        // get target and source position (method returns positions as array)
        var pos = rd.get_position();
        // display current row and current cell
        msg.innerHTML = 'Changed: ' + pos[1] + ' ' + pos[2];
    };
};


// new table using AJAX/jQuery to the drag container 
load_table = function (button) {
	// parameter (example for ajax request)
	var id = 1;
	// disable button (it can be clicked only once)
	button.style.backgroundColor = '#c0c0c0';
	button.disabled = true;
	// AJAX request
	$.ajax({
		type: 'get',
		url: 'ajax.php',
		data: 'id=' + id,
		cache: false,
		success: function (result) {
			// load new table
			$('#load_content').html(result);
			// run REDIPS.drag initialization
			rd.init();
		}
	});
};


// add onload event listener
if (window.addEventListener) {
	window.addEventListener('load', redips_init, false);
}
else if (window.attachEvent) {
	window.attachEvent('onload', redips_init);
}

function DeleteNodes(listObject, dragObject) {

    debugger;
    dataArray.splice(dataArray.indexOf($("#" + dragObject.id).attr("obj")), 1);
    $(listObject).remove();
    $("#" + dragObject.id).remove();
}

function AddNewPost() {
    debugger;
    var places = [];
    var events = [];
    var people = [];
    var countries = [];
    var activities = [];

    var test;
    var i = 0;
    for(i = 0;i < $("#contentList").children().length; i++) {
        if ($("#contentList").children()[i].type == "people") {
            people.push($("#" + $("#contentList").children()[i].id).attr("uid"));
        }
        else if ($("#contentList").children()[i].type == "activity") {
            activities.push($("#" + $("#contentList").children()[i].id).attr("uid"));
        }
        else if ($("#contentList").children()[i].type == "event") {
            events.push($("#" + $("#contentList").children()[i].id).attr("uid"));            
        }
        else if ($("#contentList").children()[i].type == "country") {
            countries.push($("#" + $("#contentList").children()[i].id).attr("uid"));
        }
        else if ($("#contentList").children()[i].type == "place") {
            places.push($("#" + $("#contentList").children()[i].id).attr("uid"));
        }
    }
//    $("#contentList").children().each(function () {
//        test += $(this).attr("type") + "_";
//    });

    var placesRes = places.join('", "');
    var eventsRes = events.join('", "');
    var peopleRes = people.join('", "');
    var countriesRes = countries.join('", "');
    var activitiesRes = activities.join('", "');

    $.ajax({
        type: "POST",
        url: "/Services/ControlLoader.asmx/AddNewPost",
        data: "{_where:'" + placesRes + "', _when:'" + $("#inputDate").attr("value") + "', _with:'" + peopleRes + "',_who:'ME', _what:'" + activitiesRes + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $('#breakingNews').prepend(msg.d);
            $.fancybox.close();
        },
        error: function (msg) {
            $('#breakingNews').html("-> " + msg.error);

        }
    });

}
